package st.spider;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

public class CatchOmdyPageItems implements PageProcessor {

	private Site site = Site.me().setDomain("www.xuandy.com/movie/")
			.setSleepTime(15);

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String[] getTags() {
		return tags;
	}

	public void setTags(String[] tags) {
		this.tags = tags;
	}

	private String[] tags;

	@SuppressWarnings("deprecation")
	public void process(Page page) {

		String html = page.getHtml().xpath("//div[@class='entry']").toString();
		// String html = page.getHtml().xpath("//div[@id='Zoom']").toString();
		System.out.println(html);

		MongoClient mongoClient;
		try {
			mongoClient = new MongoClient("115.159.74.171", 27017);
			// mongoClient = new MongoClient("localhost", 27017);
			DB db = mongoClient.getDB("cult_site");
			db.authenticate("root", new char[] { 'r', 'o', 'o', 't' });
			DBCollection dbc = db.getCollection("movie");
			DBObject o = new BasicDBObject();
			o.put("name", this.name);
			o.put("tags", this.tags);
			o.put("entityHtml", html);
			o.put("updateTime", new Date().toLocaleString());
			dbc.insert(o);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Site getSite() {
		return site;

	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		/*
		 * Spider.create(new CatchOmdyPageItems())
		 * .addUrl("http://www.xuandy.com/movie/54220.html")
		 * //.addUrl("http://www.dytt8.net/html/gndy/jddy/20150420/47885.html")
		 * .addPipeline(new ConsolePipeline()).run();
		 */

		try {
			MongoClient mongoClient = new MongoClient("localhost", 27017);
			DB db = mongoClient.getDB("cult_site");
			db.authenticate("root", new char[] { 'r', 'o', 'o', 't' });
			DBCollection dbc = db.getCollection("page");

			DBCursor c = dbc.find();
			List<DBObject> l = c.toArray();
			// List<Object> movies = new ArrayList<Object>();
			for (DBObject o : l) {
				// String tag = (String) o.get("tag");
				// String index = (String) o.get("index");
				BasicDBList items = (BasicDBList) o.get("items");
				for (int i = 0; i < items.size(); i++) {
					DBObject item = (DBObject) items.get(i);
					String href = (String) item.get("href");
					String title = (String) item.get("title");
					/*
					 * CatchOmdyPageItems temp = new CatchOmdyPageItems();
					 * temp.setName(title); temp.setTags(new String[] { "omdy"
					 * }); Spider.create(temp).addUrl(href) .addPipeline(new
					 * ConsolePipeline()).run();
					 */

					// insert
					mongoClient = new MongoClient("115.159.74.171", 27017);
					// mongoClient = new MongoClient("localhost", 27017);
					DB db1 = mongoClient.getDB("cult_site");
					db1.authenticate("root", new char[] { 'r', 'o', 'o', 't' });
					DBCollection dbc1 = db1.getCollection("movie");
					DBObject o1 = new BasicDBObject();
					o1.put("name", title.replace("《", "").replace("》高清迅雷下载", ""));
					o1.put("tags", new String[] { "omdy" });

					String html = HttpTookit.doGet(href, null,
							CharEncoding.UTF8);
					// System.out.println(html);
					Document doc = Jsoup.parse(html);
					Elements els = doc.getElementsByAttributeValue("class",
							"entry");

					String temp = els.get(0).toString();
					temp = temp
							.replace(
									"<a href=\"http://www.xuandy.com/\" title=\"炫小编\" target=\"_blank\">炫小编</a>",
									"<a href=\"user/admin\" title=\"小编\" target=\"_blank\">小编</a>");
					temp = temp
							.replace("http://www.xuandy.com/tag", "movie/tag")
							.replace(
									"下载地址：<a href=\"http://www.xuandy.com/\">http://www.xuandy.com/</a> <br />",
									"");
					System.out.println(temp);

					o1.put("entityHtml", temp);
					o1.put("updateTime", new Date().toLocaleString());
					dbc1.insert(o1);
				}
				// for (DBObject item : items) {

				// DBObject res = new BasicDBObject();
				// res.put("name", item.get("title"));
				// res.put("entityHtml", arg1);
				// res.put("tags", new String[] { "欧美电影" });
				// res.put("updateTime", new Date().toLocaleString());
				// movies.add(res);
				// }
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}